
package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.dsl.QRiskDistrict;
import com.joysuch.wwyt.risk.repository.RiskDistrictDao;
import com.mysema.commons.lang.Assert;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ImportNameToRiskDistrictConvertor implements FieldValueConvert<String, String> {

    @Autowired
    private JPAQueryFactory factory;

    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        if (value == null) {
            return null;
        }

        RiskDistrict first = factory.select(QRiskDistrict.riskDistrict).from(QRiskDistrict.riskDistrict).where(
                QRiskDistrict.riskDistrict.name.eq(value).and(QRiskDistrict.riskDistrict.state.notIn(DataStateTypes.DELETE.getState()).or(QRiskDistrict.riskDistrict.state.isNull()))
        ).fetchFirst();
        Assert.notNull(first, "风险分区[" + value + "]不存在 ");
        return first.getId().toString();
    }

}
